package org.jeecg.config.interceptor;

import cn.com.hhrcw.constant.CommonConstant;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.utils.Constant;
import cn.com.hhrcw.utils.JwtUtil;
import cn.com.hhrcw.utils.SubjectUtils;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Charsets;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * 登陆请求拦截器
 *
 * @author: taoyan
 * @date: 2020年03月20日
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //采集信息请求直接放行
        String method = request.getRequestURI();
        if("/admin/fillInInfo/add".equals(method) || "/admin/fillInInfo/getFillInInfoList".equals(method) || "/admin/fillInInfo/getFillInInfoByChannelList".equals(method)||"/personal/top/job/token".equals(method)||"/personal/top/job/list".equals(method)){
            return true;
        }
        String token = ServletUtil.getHeader(request, CommonConstant.TOKEN, Charsets.UTF_8);
        if (!redisTemplate.hasKey(CommonConstant.PREFIX_USER_TOKEN + token)) {
            ServletUtil.write(response, JSON.toJSONString(Result.error(401,"登陆失效,请重新登陆")), MediaType.APPLICATION_JSON_UTF8_VALUE);
            return false;
        }
        SysUser sysUser =(SysUser) redisTemplate.opsForValue().get(CommonConstant.PREFIX_USER_TOKEN + token);
        if (sysUser != null){
            Boolean banFlag = sysUser.getBanFlag();
            if (null != banFlag && banFlag){
                ServletUtil.write(response, JSON.toJSONString(Result.error(401,"当前账号已被禁用，请联系相关客服进行处理")), MediaType.APPLICATION_JSON_UTF8_VALUE);
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String method = request.getRequestURI();
        //非采集信息外的所有请求执行后续操作
        if(!("/admin/fillInInfo/add".equals(method) || "/admin/fillInInfo/getFillInInfoList".equals(method) || "/admin/fillInInfo/getFillInInfoByChannelList".equals(method)||"/personal/top/job/token".equals(method)||"/personal/top/job/list".equals(method))) {
            String token = ServletUtil.getHeader(request, CommonConstant.TOKEN, Charsets.UTF_8);
            //token续期  只要有请求就保持token有效期
            SysUser sysUser = (SysUser) SubjectUtils.getSubject();
            //顶端登录态，续期单独处理逻辑
            if (Constant.LOGIN_TYPE_DDXW_LOGIN.equals(sysUser.getThirdType())) {
                redisTemplate.expire(CommonConstant.PREFIX_USER_TOKEN + token, SubjectUtils.EXPIR_DAY, TimeUnit.MINUTES);
                redisTemplate.opsForValue().set(CommonConstant.PREFIX_USER_PERSONAL_TOKEN + token, sysUser, SubjectUtils.EXPIR_DAY, TimeUnit.MINUTES);
            }
            else {
                String sign = JwtUtil.sign(sysUser.getUsername(), sysUser.getPassword());
                redisTemplate.expire(CommonConstant.PREFIX_USER_TOKEN + token, SubjectUtils.EXPIR_DAY, TimeUnit.MINUTES);
                redisTemplate.opsForValue().set(CommonConstant.PREFIX_USER_PERSONAL_TOKEN + token, sign, SubjectUtils.EXPIR_DAY, TimeUnit.MINUTES);

            }
        }

    }
}
